package com.jsz.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jsz.constant.MessageConstant;
import com.jsz.entity.PageResult;
import com.jsz.entity.QueryPageBean;
import com.jsz.entity.Result;
import com.jsz.pojo.Permission;
import com.jsz.service.PermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 权限管理
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    //添加权限
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('PERMISSION_ADD')")//权限添加的权限设置
    public Result add(@RequestBody Permission permission) {
        try {
            permissionService.add(permission);
            //添加权限成功
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //添加数据失败
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }

    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = permissionService.findPage(queryPageBean);
        return pageResult;
    }

    //回显编辑弹框信息
    @RequestMapping("/findById/{id}")
    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")//权限查询的权限设置
    public Result findById(@PathVariable Integer id) {
        try {
            Permission permission = permissionService.findById(id);
            //编辑权限数据成功
            return new Result(true, MessageConstant.UPDATE_PERMISSION_SUCCESS, permission);
        } catch (Exception e) {
            e.printStackTrace();
            //编辑权限数据失败
            return new Result(false, MessageConstant.UPDATE_PERMISSION_FAIL);
        }
    }

    //修改数据
    @RequestMapping("/update")
    @PreAuthorize("hasAuthority('PERMISSION_UPDATE')")//权限修改的权限设置
    public Result update(@RequestBody Permission permission){
        try{
            permissionService.update(permission);
            //编辑成功
            return new Result(true,MessageConstant.UPDATE_PERMISSION_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            //编辑失败
            return new Result(false,MessageConstant.UPDATE_PERMISSION_FAIL);
        }
    }

    //删除权限数据
    @RequestMapping("/deleteById/{id}")
    @PreAuthorize("hasAuthority('PERMISSION_DELETE')")//权限删除的权限设置
    public Result deleteById(@PathVariable Integer id){
        try{
            permissionService.deleteById(id);
            //删除成功
            return new Result(true,MessageConstant.DELETE_PERMISSION_SUCCESS);
        }catch (RuntimeException r){
            return new Result(false,r.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            //删除失败
            return new Result(false,MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }

    //查询所有权限信息
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Permission> list=permissionService.findAll();
            //查询成功
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            //查询失败
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }
}
